/********************************************************************************
 * Project Name		[JavaEE_Components]
 * File Name     	[DeletingMails.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package javamail.mail;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Properties;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;

import org.junit.Before;
import org.junit.Test;

import javamail.utils.MailConstants;
import javamail.utils.PasswordUtils;

/**
 * 删除邮件, 更新邮件状态<br/>
 * 
 * 有关邮件状态的一些标记<br/>
 * Flags.Flag.ANSWERED<br/>
 * Flags.Flag.DELETED<br/>
 * Flags.Flag.DRAFT<br/>
 * Flags.Flag.FLAGGED<br/>
 * Flags.Flag.RECENT<br/>
 * Flags.Flag.SEEN<br/>
 * Flags.Flag.USER<br/>
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class DeletingMails {

	private static Properties properties;

	private static String emailAddress = "javamail_sns@sina.com";

	@Before
	public void init() {
		properties = new Properties();
		properties.put("mail.store.protocol", MailConstants.PROTOCAL_POP3);
		properties.put("mail.pop3.host", MailConstants.POP3_HOST_OF_SINA);
		properties.put("mail.pop3.port", MailConstants.POP3_PORT_OF_SINA);
		properties.put("mail.pop3.auth", MailConstants.IS_AUTHORIZATION_NEED);
	}

	/**
	 * 删除邮件
	 * 
	 * @throws Exception
	 */
	public static void deleteMails() throws Exception {
		Session session = Session.getInstance(properties);
		Store store = session.getStore(MailConstants.PROTOCAL_POP3);
		store.connect(emailAddress, PasswordUtils.readPassword(emailAddress));

		Folder folder = store.getFolder("INBOX");
		folder.open(Folder.READ_WRITE);

		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

		Message[] messages = folder.getMessages();
		if (messages != null && messages.length > 0) {
			for (Message message : messages) {
				String from = InternetAddress.toString(message.getFrom());
				if (from != null) {
					System.out.println("发件人: " + MimeUtility.decodeText(from));
				}

				String replayTo = InternetAddress.toString(message.getReplyTo());
				if (replayTo != null) {
					System.out.println("回复: " + MimeUtility.decodeText(replayTo));
				}

				String to = InternetAddress.toString(message.getRecipients(RecipientType.TO));
				System.out.println("收件人: " + MimeUtility.decodeText(to));

				String subject = message.getSubject();
				if (subject != null) {
					System.out.println("主题: " + MimeUtility.decodeText(subject));
				}

				Date sendDate = message.getSentDate();
				if (sendDate != null) {
					System.out.println("发送时间: " + sendDate);
				}

				System.out.println("确定要删除该邮件吗?");
				String answer = reader.readLine();
				if ("yes".equalsIgnoreCase(answer)) {
					message.setFlag(Flags.Flag.DELETED, true);
					System.out.println("删除消息成功");
				} else {
					break;
				}
			}
		}

		folder.close(true);
		store.close();
	}

	@Test
	public void testDeleteMails() throws Exception {
		deleteMails();
	}
}
